package vn.vfriends.mcms.admin.bean;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.mcms.admin.model.Site;
import vn.vfriends.mcms.admin.model.User;
import vn.vfriends.mcms.admin.service.SiteService;

/**
 *
 * @author tuan@vfriends.vn
 */
@Named
@ConversationScoped
public class SiteBean implements Serializable {

    static private Logger logger = LoggerFactory.getLogger(SiteBean.class);
    
    @Inject
    private Conversation conversation;
    
    @Inject
    private SiteService siteService;
    
    private Site site = new Site();
    
    private Site[] selectedSites;
    
    private List<Site> siteList;
    
    @PostConstruct
    public void init() {
        siteList = siteService.findSites();
    }

    public List<Site> getSiteList() {
        return siteList;
    }

    public Conversation getConversation() {
        return conversation;
    }

    public Site getSite() {
        return site;
    }

    public void setSite(Site site) {
        this.site = site;
    }

    public Site[] getSelectedSites() {
        return selectedSites;
    }

    public void setSelectedSites(Site[] selectedSites) {
        this.selectedSites = selectedSites;
    }

    public void preNewAction() {
        site = new Site();

        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public void preDeleteAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            if (this.selectedSites == null || this.selectedSites.length == 0) {
                if (RequestContext.getCurrentInstance() != null) {
                    RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                }
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Hãy chọn site", ""));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }

    public void saveAction() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
            HttpSession session = request.getSession(false);
            User loggedUser = (User) session.getAttribute("LOGGED_USER");
            
            site.setUser(loggedUser);
            
            this.siteService.createSite(site);
            context.getExternalContext().redirect("list.jsf");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }

    public void deleteAction() {
        try {
            for (Site selectedSite : selectedSites) {
                this.siteService.destroySite(selectedSite.getId());
            }
            if (!conversation.isTransient()) {
                conversation.end();
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("list.jsf");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }

    public void siteRowEditListener(RowEditEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            Site editSite = (Site) event.getObject();
            if (editSite == null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Site không tồn tại", ""));
            } else {
                this.siteService.editSite(editSite);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }
    }
}
